package cn.mickey.exchange.util;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

public class PageUtil<M extends BaseMapper<T>, T> {

    /**
     * 通用分页
     * 缺点:由于map是无序的,所以返回的数据也是无序,但不影响数据传递给前端
     *
     * @param baseMapper
     * @param params
     * @param columns
     * @return
     */
    public IPage pageList(M baseMapper, Map<String, Object> params, String... columns) {
        Integer no = Convert.convert(Integer.class, params.get("no"));
        Integer size = Convert.convert(Integer.class, params.get("size"));
        Long total = baseMapper.selectCount(new QueryWrapper<T>().select("*"));
        QueryWrapper<T> qw = new QueryWrapper<>();
        qw.select(columns);
        IPage mapsPage = null;
        if (total > 0) {
            mapsPage = baseMapper.selectMapsPage(new Page<>(no, size), qw);
        }
        return mapsPage;
    }

    /**
     * 获取当前页
     * @param page
     * @return
     */
    public static int getPage(Integer page) {
        int p = 1;
        if (null != page) {
            p = page > 0 ? page : 1;
        }
        return p;
    }

    /**
     * 获取分页条数
     * @param nums
     * @return
     */
    public static int getNum(Integer nums) {
        int n = 8;
        if (null != nums) {
            n = nums > 0 ? nums : 8;
        }
        return n;
    }


}
